<%@ page import="beans.Vehicle" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>公司用车管理系统（司机队长）- 车辆信息</title>
  <style>
    /* 全局样式 */
    body {
      margin: 0;
      padding: 0;
      font-family: "Microsoft YaHei", Arial, sans-serif;
      background: #f6f6f6;
      color: #333;
    }

    /* 头部样式 */
    .header {
      background: #183a8a;
      color: #fff;
      font-size: 2rem;
      font-weight: bold;
      padding: 10px 0 10px 10px;
      letter-spacing: 2px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .header-right {
      display: flex;
      align-items: center;
      margin-right: 30px;
      font-size: 1rem;
    }
    .user-icon {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background: #fff;
      color: #183a8a;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 2rem;
      margin-right: 10px;
    }
    .user-info {
      margin-right: 20px;
      color: #fff;
    }
    .bell-icon {
      font-size: 1.3rem;
      margin-right: 20px;
      color: #fff;
    }
    .logout {
      color: #fff;
      text-decoration: none;
      margin-left: 10px;
      font-size: 1rem;
    }

    /* 主体容器 */
    .container {
      display: flex;
      height: calc(100vh - 56px);
    }

    /* 侧边栏样式 */
    .sidebar {
      width: 220px;
      background: #183a8a;
      display: flex;
      flex-direction: column;
      padding-top: 10px;
      box-shadow: 2px 0 8px rgba(24,58,138,0.05);
    }
    .menu-item {
      color: #fff;
      padding: 18px 0 18px 32px;
      font-size: 1.15rem;
      cursor: pointer;
      border-left: 5px solid transparent;
      transition: background 0.2s, border 0.2s;
      text-decoration: none;
      display: block;
    }
    .menu-item.active, .menu-item:hover {
      background: #2450a7;
      border-left: 5px solid #fff;
      font-weight: bold;
      box-shadow: 0 4px 8px rgba(24,58,138,0.10);
    }

    /* 主内容区样式 */
    .main-content {
      flex: 1;
      background: #fff;
      border: 2px solid #3ba0e9;
      border-left: none;
      padding: 30px;
      overflow-y: auto;
    }
    .page-title {
      font-size: 2.2rem;
      color: #183a8a;
      margin-bottom: 30px;
      padding-bottom: 15px;
      border-bottom: 2px solid #eee;
    }
    .action-bar {
      display: flex;
      gap: 20px;
      margin-bottom: 25px;
    }
    .action-btn {
      padding: 8px 20px;
      background: #1da1e7;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 1.1rem;
      cursor: pointer;
      transition: background 0.2s;
      text-decoration: none;
      display: inline-block;
    }
    .action-btn:hover {
      background: #2176b8;
    }
    .divider {
      height: 1px;
      background: #ddd;
      margin: 20px 0;
    }

    /* 搜索框样式 */
    .search-bar {
      display: flex;
      gap: 10px;
      margin-bottom: 20px;
      flex-wrap: wrap;
    }
    .search-field {
      padding: 8px 12px;
      border: 1px solid #ddd;
      border-radius: 4px;
      width: 180px;
    }
    .search-btn {
      background: #1da1e7;
      color: white;
      border: none;
      padding: 8px 20px;
      border-radius: 4px;
      cursor: pointer;
    }

    /* 表格样式 */
    .driver-table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    .driver-table th {
      background: #f0f4f8;
      padding: 12px 15px;
      text-align: left;
      font-weight: bold;
      border-bottom: 2px solid #ddd;
    }
    .driver-table td {
      padding: 12px 15px;
      border-bottom: 1px solid #eee;
    }
    .driver-table tr:hover {
      background-color: #f5f9ff;
    }

    /* 状态标签样式 */
    .status-badge {
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 0.9rem;
    }
    .status-空闲 {
      background: #d4edda;
      color: #155724;
    }
    .status-使用 {
      background: #fff3cd;
      color: #856404;
    }

    /* 响应式设计 */
    @media (max-width: 900px) {
      .sidebar {
        width: 120px;
      }
      .menu-item {
        padding-left: 15px;
        font-size: 1rem;
      }
      .action-bar {
        flex-direction: column;
        gap: 10px;
      }
      .search-bar {
        flex-direction: column;
        align-items: stretch;
      }
      .search-field {
        width: 100%;
      }
      .driver-table {
        display: block;
        overflow-x: auto;
      }
    }
  </style>
</head>
<body>
  <div class="header">
    <div>欢迎登录公司用车管理系统（司机队长）</div>
    <div class="header-right">
      <div class="user-icon">👤</div>
      <div class="user-info">账号：<%=session.getAttribute("username")%></div>
      <div class="bell-icon">🔔</div>
      <a href="${pageContext.request.contextPath}/LogoutServlet" class="logout">退出登录</a>
    </div>
  </div>

  <div class="container">
    <div class="sidebar">
      <a class="menu-item" href="DriversServlet?action=all3">系统首页</a>
      <a class="menu-item" href="DriversServlet?action=all2">个人中心</a>
      <a class="menu-item" href="ShenqingServlet?action=all">我的待办</a>
      <a class="menu-item" href="UseOrderServlet?action=list">我处理的</a>
      <a class="menu-item active" href="VehiclesServlet?action=all">全部车辆信息</a>
      <a class="menu-item" href="DriversServlet?action=all">全部司机信息</a>
    </div>

    <div class="main-content">
      <h1 class="page-title">车辆信息管理</h1>

      <div class="action-bar">
        <a href="VehiclesServlet?action=add" class="action-btn">增加车辆</a>
      </div>

      <% if(request.getAttribute("message") != null) { %>
        <div class="alert alert-success"><%=request.getAttribute("message")%></div>
      <% } %>
      <% if(request.getAttribute("error") != null) { %>
        <div class="alert alert-danger"><%=request.getAttribute("error")%></div>
      <% } %>

      <!-- 搜索表单 -->
      <form action="VehiclesServlet" method="get" class="search-bar">
        <input type="hidden" name="action" value="search">
        <input type="text" name="licensePlate" placeholder="车牌号" class="search-field" value="${param.licensePlate}">
        <input type="text" name="brandModel" placeholder="品牌型号" class="search-field" value="${param.brandModel}">
        <select name="status" class="search-field">
          <option value="">全部状态</option>
          <option value="空闲" ${param.status == '空闲' ? 'selected' : ''}>空闲</option>
          <option value="使用" ${param.status == '使用' ? 'selected' : ''}>使用</option>
        </select>
        <button type="submit" class="search-btn">搜索</button>
        <a href="VehiclesServlet?action=all" class="action-btn">重置</a>
      </form>

      <div class="divider"></div>

      <table class="driver-table">
        <thead>
        <tr>
          <th>车辆ID</th>
          <th>车牌号</th>
          <th>品牌型号</th>
          <th>颜色</th>
          <th>购买日期</th>
          <th>状态</th>
          <th>座位数</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
          <% 
          List<Vehicle> vehicleList = (List<Vehicle>)request.getAttribute("vehicleList");
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          if(vehicleList != null && !vehicleList.isEmpty()) {
            for(Vehicle vehicle : vehicleList) { 
          %>
            <tr>
          <tr>
            <td><%= vehicle.getVehicleId() %></td>
            <td><%= vehicle.getLicensePlate() %></td>
            <td><%= vehicle.getBrandModel() %></td>
            <td><%= vehicle.getColor() %></td>
            <td><%= vehicle.getPurchaseDate() != null ? sdf.format(vehicle.getPurchaseDate()) : sdf.format(new java.util.Date()) %></td>
            <td><%= vehicle.getStatus() %></td>
            <td><%= vehicle.getNumber() %></td>
              <td>
                <a href="VehiclesServlet?action=detail&vehicleId=<%= vehicle.getVehicleId() %>"
                   class="action-btn">编辑</a>
                <a href="VehiclesServlet?action=delete&vehicleId=<%= vehicle.getVehicleId() %>&vehicleName=<%= vehicle.getLicensePlate() %>"
                   onclick="return confirm('确定删除车辆 <%= vehicle.getLicensePlate() %> 吗？') " class="action-btn">删除</a>
              </td>
            </tr>
          <% } } else { %>
            <tr>
              <td colspan="6" style="text-align: center;">暂无车辆信息</td>
            </tr>
          <% } %>
        </tbody>
      </table>
    </div>
  </div>

  <script>
    // 删除车辆
    function deleteVehicle(id) {
      if(confirm('确定要删除这辆车吗？此操作不可恢复。')) {
        window.location.href = '${pageContext.request.contextPath}/VehiclesServlet?action=delete&id=' + id;
      }
    }
  </script>
</body>
</html>